// 解析上下文里node原生请求的POST参数
function parsePostData(ctx) {
    return new Promise((resolve, reject) => {
        try {
            let postdata = "";
            ctx.req.addListener('data', (data) => {
                postdata += data
            })
            ctx.req.addListener("end", function () {
                let parseData = parseQueryStr(postdata)
                resolve(parseData)
            })
        } catch (err) {
            reject(err)
        }
    })
}

// 将POST请求参数字符串解析成JSON
function parseQueryStr(queryStr) {
    let queryData = {}
    let queryStrList = queryStr.split('&')
    console.log(queryStrList)
    for (let [index, queryStr] of queryStrList.entries()) {
        let itemList = queryStr.split('=')
        queryData[itemList[0]] = decodeURIComponent(itemList[1])
    }
    return queryData
}

function formatDBObj(dbObj,config){
    let obj = {};
    Object.keys(dbObj).forEach(function(key){
        obj[config[key]] = dbObj[key];
   });
   return obj;
}
function formatDBList(list,config){
    return list.map(function(val){
        formatDBObj(val,config);
    })
}
// module.exports.formatDBList = formatDBList;
module.exports.parsePostData = parsePostData;
